package com.cx.service.impl;

import com.cx.mapper.DepartmentMapper;
import com.cx.pojo.Department;
import com.cx.pojo.DepartmentPages;
import com.cx.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements DepartmentService {

    private final static int PAGE_SIZE = 5;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public PageInfo<Department> findAll(DepartmentPages departmentPages) {
        PageHelper.startPage(departmentPages.getCurrentPage(),PAGE_SIZE);
        PageInfo<Department> page = new PageInfo<Department>(departmentMapper.findAllBySn(departmentPages));
        return page;
    }

    @Override
    public Department findById(int id) {
        return departmentMapper.findById(id);
    }

    @Override
    @Transactional
    public int deleteById(int id) {
        return departmentMapper.deleteById(id);
    }

    @Override
    @Transactional
    public int addDept(Department department) {
        department.setManagerId(department.getEmployee().getId());
        int result = departmentMapper.addDept(department);
        department.setDirPath("/" + department.getId());
        List<Department> all = departmentMapper.findAll();
        for(Department department1 : all){
            if (department.getParentId().equals(department1.getId())){
                department.setDirPath(department1.getDirPath() + department.getDirPath());
            }
        }
        departmentMapper.updateDept(department);
        return result;
    }

    @Override
    @Transactional
    public int updateDept(Department department) {
        department.setManagerId(department.getEmployee().getId());
        department.setDirPath("/" + department.getId());
        List<Department> all = departmentMapper.findAll();
        if (department.getParentId() == null){

        } else {

            for(Department department1 : all){
                if (department.getParentId().equals(department1.getId())){
                    department.setDirPath(department1.getDirPath() + department.getDirPath());
                }
            }
        }

        return departmentMapper.updateDept(department);
    }

    @Override
    @Transactional
    public void patchDel(List<Integer> ids) {
        departmentMapper.patchDel(ids);
    }

    @Override
    public List<Department> getDeptTree() {
        List<Department> all = departmentMapper.findAll();
        HashMap<Long,Department> depts = new HashMap<>();
        List<Department> deptTree = new ArrayList<>();
        // 因为对象是名是引用数据类型，所有可以通过对应地址值而改变值
        for (Department department : all) {
            depts.put(department.getId(),department);
        }

        for (Department department : all) {
            if (department.getParentId() == null){
                deptTree.add(department);
            } else {
                Long parentId = department.getParentId();

                Department deptParent = depts.get(parentId);

                deptParent.getChildren().add(department);
            }
        }

        return deptTree;
    }
}
